package cn.com.hbhgkj.webapp.action;

import java.util.List;

import cn.com.hbhgkj.dao.SearchException;
import cn.com.hbhgkj.model.CopyrightRegisteration;
import cn.com.hbhgkj.service.ApplicationSendException;
import cn.com.hbhgkj.service.CopyrightRegisterationManager;
import cn.com.hbhgkj.service.SynchronizeException;

import com.opensymphony.xwork2.Preparable;

public class CopyrightRegisterationAction extends BaseAction implements
		Preparable {
	private CopyrightRegisterationManager copyrightRegisterationManager;
	private List copyrightRegisterations;
	private CopyrightRegisteration copyrightRegisteration;
	private Long id;
	private String query;

	public void setCopyrightRegisterationManager(
			CopyrightRegisterationManager copyrightRegisterationManager) {
		this.copyrightRegisterationManager = copyrightRegisterationManager;
	}

	public List getCopyrightRegisterations() {
		return copyrightRegisterations;
	}

	/**
	 * Grab the entity from the database before populating with request
	 * parameters
	 */
	public void prepare() {
		if (getRequest().getMethod().equalsIgnoreCase("post")) {
			// prevent failures on new
			String copyrightRegisterationId = getRequest().getParameter(
					"copyrightRegisteration.id");
			if (copyrightRegisterationId != null
					&& !copyrightRegisterationId.equals("")) {
				copyrightRegisteration = copyrightRegisterationManager
						.get(new Long(copyrightRegisterationId));
			}
		}
	}

	public void setQ(String q) {
		this.query = q;
	}

	public String getQuery() {
		return query;
	}

	public String list() {
		try {
			copyrightRegisterations = copyrightRegisterationManager.search(
					query, CopyrightRegisteration.class);
		} catch (SearchException se) {
			addActionError(se.getMessage());
			copyrightRegisterations = copyrightRegisterationManager.getAll();
		}
		return SUCCESS;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public CopyrightRegisteration getCopyrightRegisteration() {
		return copyrightRegisteration;
	}

	public void setCopyrightRegisteration(
			CopyrightRegisteration copyrightRegisteration) {
		this.copyrightRegisteration = copyrightRegisteration;
	}

	public String delete() {
		copyrightRegisterationManager.remove(copyrightRegisteration.getId());
		saveMessage(getText("copyrightRegisteration.deleted"));

		return SUCCESS;
	}

	public String edit() {
		if (id != null) {
			copyrightRegisteration = copyrightRegisterationManager.get(id);
		} else {
			copyrightRegisteration = new CopyrightRegisteration();
		}

		return SUCCESS;
	}

	public String save() throws Exception {
		if (cancel != null) {
			return "cancel";
		}

		if (delete != null) {
			return delete();
		}

		boolean isNew = (copyrightRegisteration.getId() == null);

		copyrightRegisterationManager.save(copyrightRegisteration);

		String key = (isNew) ? "copyrightRegisteration.added"
				: "copyrightRegisteration.updated";
		saveMessage(getText(key));

		if (!isNew) {
			return INPUT;
		} else {
			return SUCCESS;
		}
	}

	public String synchronize() {
		try {
			copyrightRegisterationManager.synchronize();
		} catch (SynchronizeException e) {
			addActionError(e.getMessage());
		} catch (ApplicationSendException e) {
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}
}